package com.wframe.project.jnd.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.frame.database.JdataBean;
import com.wframe.framework.database.BaseDao;

@Repository
public class CommonDao extends BaseDao{

	public void saveSmsCode(String phone,String code){
		int n=execute("update wb_sms_code set checknum=?,ischeck=0,sendtime=now() where phone=?",new Object[]{code,phone});
		if(n==0){
			execute("insert into wb_sms_code(checknum,ischeck,sendtime,phone) values (?,0,now(),?)",new Object[]{code,phone});
		}
	}
	public void updateSmsCode(String phone,boolean ischeck){
		execute("update wb_sms_code set ischeck=? where phone=?",new Object[]{ischeck?1:0,phone});
	}
	public JdataBean getSmsCode(String phone){
		return getObject("select * from wb_sms_code where phone=?", new Object[]{phone});
	}
	public List<JdataBean> queryPhotos(String dataid) throws Exception{
		if(StringUtils.isBlank(dataid))return new ArrayList<JdataBean>();
		return querySql("select * from app_pictures where dataid=? and deltag=0 order by orderno,id", new Object[]{dataid});
	}
	public List<Map> queryPhotosMap(String dataid) throws Exception{
		if(StringUtils.isBlank(dataid))return new ArrayList<Map>();
		return querySqlMap("select * from app_pictures where dataid=? and deltag=0 order by orderno,id", new Object[]{dataid});
	}

	public JdataBean getSyspro(){
		List<JdataBean> list=querySql("select * from wb_sys_pro", null);
		JdataBean bean=new JdataBean();
		for(JdataBean b:list)
			bean.put(b.getString("code"), b.getString("val"));
		return bean;
	}
	public List<JdataBean> getInformaTion(String code){
		List<JdataBean> list = querySql("SELECT * FROM(SELECT * FROM wb_issue WHERE CODE =? AND (issuetime IS NULL OR issuetime <= NOW())AND (INVALIDTIME IS NULL OR INVALIDTIME >= NOW())AND state = 1 ORDER BY INVALIDTIME ASC, issuetime DESC) t GROUP BY t.title", new Object[]{code});
		List<JdataBean> datas = new ArrayList<>();
		for(int i = 0; i < list.size(); i++){
			datas.add(list.get(i));
		}
		return datas;
	}
}
